package cn.itcast.service;

import cn.itcast.common.utils.Page;
import cn.itcast.mapper.CustomerMapper;
import cn.itcast.pojo.Customer;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by Administrator on 2018/5/12.
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    //客户列表
    @Override
    public Page<Customer> findCustomerList(Integer page, Integer rows,
                                           String customer, String customerSource, String customerIndustry, String customerlevel) {
        //创建客户对象
        Customer customer1 = new Customer();

        //判断客户名称
        if(org.apache.commons.lang3.StringUtils.isNotBlank(customer)){
            //判断某字符串是否不为空且长度不为0且不由空白符(whitespace) 构成，等于 !isBlank(String str) 如果为空则返回空
            customer1.setCustName(customer);
        }
        //判断客户来源
        if(org.apache.commons.lang3.StringUtils.isNotBlank(customerSource)){

            customer1.setCustSource(customerSource);
        }
        //判断客户所属行业
        if(org.apache.commons.lang3.StringUtils.isNotBlank(customerIndustry)){

            customer1.setCustIndustry(customerIndustry);
        }
        //判断客户级别
        if(org.apache.commons.lang3.StringUtils.isNotBlank(customerlevel)){

            customer1.setCustLevel(customerlevel);
        }
        //当前页
        customer1.setStart((page-1)*rows);

        //每页数
        customer1.setRows(rows);

        //查询客户列表
        List<Customer> customerList = customerMapper.secectCustomerlist(customer1.getCustName(),customer1.getCustSource(),
                customer1.getCustIndustry(),customer1.getCustLevel(),customer1.getStart(),customer1.getRows());

        //查询客户列表总记录数
        Integer count = customerMapper.secectCustomerlistCount(customer1);

        //创建page返回对象
        Page<Customer> customerPage = new Page<>();

        customerPage.setTotal(count);
        customerPage.setPage(page);
        customerPage.setSize(rows);
        customerPage.setRows(customerList);

        return customerPage;
    }

    //创建用户
    @Override
    public Integer createCustomer(Customer customer) {
//        return customerMapper.creatCustomer(customer.getCustName(),customer.getCustUserId(),customer.getCustCreateId(),
//                customer.getCustSource(),customer.getCustIndustry(),customer.getCustLevel(),customer.getCustLinkman(),customer.getCustPhone(),
//                customer.getCustMobile(),customer.getCustZipcode(),customer.getCustAddress(),(Timestamp)customer.getCustCreatetime());

        return customerMapper.insertSelective(customer);
    }

    //通过id查用户
    @Override
    public Customer findCustomweById(Integer id) {

        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateCustomer(Customer customer) {

        return customerMapper.updateByPrimaryKey(customer);

    }
    //删除用户
    @Override
    public Integer delateCustomer(Integer id) {
        return customerMapper.deleteByPrimaryKey(id);
    }
}
